//
//  PGBaseViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/16.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGBaseViewController.h"



@interface PGBaseViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**contentView*/
@property (nonatomic, weak) UIView *contentView;

/**contentCollectionView*/
@property (nonatomic, weak) UICollectionView *contentCollectionView;

/**标题titleView*/
@property (nonatomic, weak) UIView *titleView;

/**标题下面滑动的线*/
@property (nonatomic, weak) UIView *lineView;

/**上一个被选中的按钮*/
@property (nonatomic, strong) UIButton *preTitleButton;

/**标题按钮的数组*/
@property (nonatomic, strong) NSMutableArray *titleButtons;

/**isInit*/
@property (nonatomic, assign) BOOL isInit;

@end

@implementation PGBaseViewController

static NSString const*itemID = @"cell";

#pragma mark -懒加载标题按钮数组
- (NSMutableArray *)titleButtons {
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    //创建内容的view
    [self setupContentView];
    
    //创建collectionView
    [self setUpCollectionView];
    
    
//    self.view.backgroundColor = commonBgColor;
    
    
    //添加自控制器
//    [self addChildController];
    
   
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (_isInit == NO) {
        //创建标题titleView
        [self setUpTitleView];
        _isInit = YES;
    }
    
    
}



#pragma mark -创建存放子控件的ContentView
- (void)setupContentView {
    UIView *contentView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:contentView];
    _contentView = contentView;
}


- (void)setUpTitleView {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, navigationBarHeight, SCREENW, titleHeight)];
    titleView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [_contentView addSubview:titleView];
    
    _titleView = titleView;
    
    NSInteger count =self.childViewControllers.count;
    for (NSInteger i=0; i<count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        titleButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [titleButton setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        CGFloat titleButtonW = 1.0*SCREENW/count;
        CGFloat titleButtonH = _titleView.pg_height;
        CGFloat titleButtonX = i*titleButtonW;
        CGFloat titleButtonY = 0;
        titleButton.frame = CGRectMake(titleButtonX, titleButtonY, titleButtonW, titleButtonH);
        [titleView addSubview:titleButton];
        [self.titleButtons addObject:titleButton];
        
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchDown];
        titleButton.tag = i;
        if (i == 0) {
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = [UIColor redColor];
            [titleView addSubview:lineView];
            _lineView = lineView;
            [self titleButtonClick:titleButton];
        }
    }
}

//监听按钮点击触发的方法
- (void)titleButtonClick:(UIButton *)titleButton {
    
    
    //标记按钮选中
    [self buttonSeleted:titleButton];
    
    [_contentCollectionView setContentOffset:CGPointMake(titleButton.tag*SCREENW, 0) animated:YES];
    
}

//标记按钮选中
- (void)buttonSeleted:(UIButton *)titleButton {
    titleButton.selected = YES;
    _preTitleButton.selected = NO;
    _preTitleButton = titleButton;
    
    
    CGSize titleSize = [titleButton.titleLabel.text sizeWithAttributes:@{
                                                                         NSFontAttributeName:[UIFont systemFontOfSize:15.0]
                                                                         }];
    CGFloat lineViewW = titleSize.width;
    CGFloat lineViewH = 2;
    CGFloat lineViewX = titleButton.pg_centerX;
    CGFloat lineViewY = _titleView.pg_height-lineViewH;
    
    _lineView.pg_width = lineViewW;
    _lineView.pg_height = lineViewH;
    _lineView.pg_y = lineViewY;
    
    [UIView animateWithDuration:0.25 animations:^{
        _lineView.pg_centerX = lineViewX;
    }];
}

#pragma mark -监听scrollView滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    
    NSUInteger index = offsetX/SCREENW;
    
    UIButton *selectedButton = self.titleButtons[index];
    //标记按钮选中
    [self buttonSeleted:selectedButton];
}


#pragma mark -创建内容滚动view
- (void)setUpCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = ({
        flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
        flowLayout;
    });
    
    
    UICollectionView *contentCollectionView = ({
        contentCollectionView = [[UICollectionView alloc] initWithFrame:_contentView.bounds collectionViewLayout:flowLayout ];
//        NSLog(@"_contentView%@",NSStringFromCGRect(_contentView.frame));
//        NSLog(@"contentCollectionView%@",NSStringFromCGRect(contentCollectionView.frame));
        contentCollectionView.delegate = self;
        contentCollectionView.dataSource = self;
        [contentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        contentCollectionView.showsHorizontalScrollIndicator = NO;
        contentCollectionView.showsVerticalScrollIndicator = NO;
        contentCollectionView.alwaysBounceHorizontal = YES;
        contentCollectionView.alwaysBounceVertical = NO;
        contentCollectionView.pagingEnabled = YES;
        contentCollectionView.contentSize = CGSizeMake(self.childViewControllers.count*SCREENW, 0);
        //        contentCollectionView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
        contentCollectionView.backgroundColor = commonBgColor;
        contentCollectionView;
    });
    
    [_contentView addSubview:contentCollectionView];
    _contentCollectionView = contentCollectionView;
    
//    NSLog(@"_contentCollectionView%@",NSStringFromCGRect(contentCollectionView.frame));
}



#pragma mark -UICollectionView datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [item.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    item.backgroundColor = [UIColor greenColor];
    UIViewController *vc = self.childViewControllers[indexPath.row];
//    NSLog(@"UICollectionViewCell%@",NSStringFromCGRect(item.frame));
//    NSLog(@"view%@",NSStringFromCGRect(vc.view.frame));
//    CGRect temp = vc.view.frame;
//    temp.origin.y = 0;
//    vc.view.frame = temp;
//    NSLog(@"%@",vc);
    vc.view.frame = [UIScreen mainScreen].bounds;
    [item.contentView addSubview:vc.view];
    return item;
}


//#pragma mark -添加子控制器
//- (void)addChildController {
//    //添加全部控制器
//    PGAllViewController *all = [[PGAllViewController alloc] init];
//    all.title = @"全部";
//    //    all.view.backgroundColor = [UIColor brownColor];
//    [self addChildViewController:all];
//    
//    //添加视频控制器
//    PGVideoViewController *video = [[PGVideoViewController alloc] init];
//    video.title = @"视频";
//    //    video.view.backgroundColor = [UIColor redColor];
//    [self addChildViewController:video];
//    
//    //添加声音控制器
//    PGVoiceViewController *voice = [[PGVoiceViewController alloc] init];
//    voice.title = @"声音";
//    //    voice.view.backgroundColor = [UIColor greenColor];
//    [self addChildViewController:voice];
//    
//    //添加图片全部控制器
//    PGPictureViewController *picture = [[PGPictureViewController alloc] init];
//    picture.title = @"图片";
//    //    picture.view.backgroundColor = [UIColor grayColor];
//    [self addChildViewController:picture];
//    
//    //添加段子控制器
//    PGTextViewController *text = [[PGTextViewController alloc] init];
//    text.title = @"段子";
//    //    text.view.backgroundColor = [UIColor purpleColor];
//    [self addChildViewController:text];
//}





@end
